package com.ittsang.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ittsang.dto.PageDTO;
import com.ittsang.dto.R;
import com.ittsang.dto.UserDTO;
import com.ittsang.mapper.*;
import com.ittsang.pojo.Club;
import com.ittsang.pojo.Department;
import com.ittsang.pojo.User;
import com.ittsang.pojo.UserClubPosts;
import com.ittsang.service.ClubService;
import com.ittsang.utils.UserHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class ClubServiceImpl implements ClubService {

    @Autowired
    private ClubMapper clubMapper;

    @Autowired
    private UserClubPostsMapper userClubPostsMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private DepartmentMapper departmentMapper;

    @Autowired
    private PostsMapper postsMapper;

    @Override
    public R getUserClub() {

        UserDTO user = UserHolder.getUser();

        User user1 = userMapper.getUser(user.getTelephone());

        List<Club> club = clubMapper.getUserClubList(user1.getClubId());

        for (Club club1 : club) {
            UserClubPosts num = userClubPostsMapper.getUserRole(Math.toIntExact(user.getId()), club1.getId());
            club1.setRole(num != null);
        }

        return R.ok("获取成功").put("club", club);
    }

    @Override
    public R getClubUserList(PageDTO pageDTO) {

        //分页操作
        int pageNum = pageDTO.getPageNum();
        int pageSize = pageDTO.getPagesize();
        PageHelper.startPage(pageNum,pageSize);
        int skip= (pageNum-1)*pageSize;

        UserDTO user = UserHolder.getUser();
        // 查询职位表，看用户是会长还是部长
        UserClubPosts userClubPosts = userClubPostsMapper.getUserRole(Math.toIntExact(user.getId()), user.getClubId());
        if (userClubPosts == null) {
            return R.error("用户无权限");
        }
        if (userClubPosts.getPostsId() == 0) {
            // 该用户是会长职位，直接查询全部用户
            List<User> users = userMapper.getClubUserList(user.getClubId());
            for (User user1 : users) {
                user1.setPostsId(-1);
                // 判断是否是本人
                if (user1.getId() == Math.toIntExact(user.getId())) {
                    user1.setMy(true);
                }
                // 判断是否是会长
                if (user1.getDepartmentId() == 0) {
                    user1.setDepartment("会长团");
                    user1.setRole(1);
                    user1.setPostsId(0);
                    continue;
                }
                UserClubPosts userRole = userClubPostsMapper.getUserRole(user1.getId(), user1.getClubId());
                // 判断是否有职位
                if (userRole != null) {
                    user1.setRole(1);
                    user1.setDepartment(postsMapper.getPostNameById(userRole.getPostsId()));
                    user1.setPostsId(userRole.getPostsId());
                    continue;
                }
                Department department = departmentMapper.getDepartment(user.getClubId(), user1.getDepartmentId());
                user1.setDepartment(department.getName());
            }
            Club club = new Club();
            club.setId(user.getClubId());
            club.setName(clubMapper.getClubName(user.getClubId()));
            Page<User> page = new Page<>(pageNum, pageSize);
            page.setTotal(users.size());
            List<User> userList = users.stream().skip(skip)
                    .limit(pageSize).collect(Collectors.toList());
            page.addAll(userList);
            return R.ok("获取成功").put("user", page).put("club", club).put("total", page.getTotal());
        }

        // 此逻辑是用户为部长时，只需要社团id和部门id就可以了
        User currentUser = userMapper.getUser(user.getTelephone());
        List<User> users = userMapper.getClubUserListByDepartment(currentUser.getDepartmentId(), currentUser.getClubId());
        Department department1 = departmentMapper.getDepartment(currentUser.getClubId(), currentUser.getDepartmentId());
        for (User user1 : users) {
            // 判断是否是本人
            if (user1.getId() == Math.toIntExact(user.getId())) {
                user1.setMy(true);
            }
            UserClubPosts userRole = userClubPostsMapper.getUserRole(user1.getId(), user1.getClubId());
            // 判断是否有职位
            if (userRole != null) {
                user1.setRole(1);
                user1.setDepartment(postsMapper.getPostNameById(userRole.getPostsId()));
                user1.setPostsId(userRole.getPostsId());
                continue;
            }
            Department department = departmentMapper.getDepartment(user.getClubId(), user1.getDepartmentId());
            user1.setPostsId(-1);
            user1.setDepartment(department.getName());
        }
        Page<User> page = new Page<>(pageNum, pageSize);
        page.setTotal(users.size());
        List<User> userList = users.stream().skip(skip)
                .limit(pageSize).collect(Collectors.toList());
        page.addAll(userList);
        return R.ok("获取成功").put("user", page).put("club", department1).put("total", page.getTotal());
    }

    @Override
    public R getCLubDep(Integer clubId) {
        if (clubId != 0) {
            List<Department> departments = departmentMapper.getDepList(clubId);
            return R.ok("获取社团部门信息成功").put("department", departments);
        }
        UserDTO userDTO = UserHolder.getUser();
        User user = userMapper.getUser(userDTO.getTelephone());
        Department department = new Department();
        department.setId(0);
        department.setClubId(user.getClubId());
        department.setName("会长团");
        department.setStatus(false);
        List<Department> departments = departmentMapper.getDepList(user.getClubId());
        departments.add(department);
        return R.ok("获取社团部门信息成功").put("department", departments);
    }

    @Override
    public R getClubDepUserList(PageDTO pageDTO) {
        UserDTO userDTO = UserHolder.getUser();
        User user = userMapper.getUser(userDTO.getTelephone());
        if (user.getClubId() != 0 || user.getPostsId() == 0) {
            List<User> users = userMapper.getClubUserListByDepartment(pageDTO.getDepartmentId(), user.getClubId());

            //分页操作
            int pageNum = pageDTO.getPageNum();
            int pageSize = pageDTO.getPagesize();
            PageHelper.startPage(pageNum,pageSize);
            int skip= (pageNum-1)*pageSize;
            Page<User> page = new Page<>(pageNum, pageSize);
            page.setTotal(users.size());
            List<User> userList = users.stream().skip(skip)
                    .limit(pageSize).collect(Collectors.toList());
            page.addAll(userList);
            return R.ok("获取社团成员成功").put("user", page);
        }
        return R.error("用户无权限");
    }

    @Override
    public R getAllClub(PageDTO pageDTO) {
        int pageNum = pageDTO.getPageNum();
        int pageSize = pageDTO.getPagesize();
        PageHelper.startPage(pageNum,pageSize);
        int skip= (pageNum-1)*pageSize;

        List<Club> clubs = clubMapper.getAllClub();
        if (UserHolder.getUser() != null) {
            UserDTO user = UserHolder.getUser();
            for (Club club : clubs) {
                if (Objects.equals(club.getId(), user.getClubId())) {
                    club.setUserStatus(1);
                }
            }
        }
        Page<Club> page = new Page<>(pageNum, pageSize);
        List<Club> clubs1 = clubs.stream().skip(skip)
                .limit(pageSize).collect(Collectors.toList());
        page.addAll(clubs1);
        return R.ok("获取社团成功").put("clubs", page);
    }

    @Override
    public R getCLubDepByClubId(Integer clubId) {
        List<Department> departments = departmentMapper.getDepList(clubId);
        return R.ok("获取社团部门信息成功").put("department", departments);
    }
}
